#!/usr/local/bin/ruby
#
#  Copyright (c) 2009 Mu Dynamics, Inc. All rights reserved.

class ResultsController < ApplicationController

  attr_accessor :test_id, :run_id
  
  # GET /results
  # GET /results.xml
  def index
    @latest = false
    @passed = 0
    @failed = 0
    @total = 0
    @test_id = nil
    @run_id = nil
    @name = nil
    if params.has_key?('latest')
      @latest = true
    end

   find_params = { :include => params[:id] }.merge( order_by )

   if !params[:name].nil? and params[:run_id].nil?
      @name = params[:name]
      logger.debug "call find_by_name"
      @results = Result.find_by_name(params[:name], @latest, find_params)
   elsif !params[:name].nil? and !params[:run_id].nil?
      @name = params[:name]
      @run_id = params[:run_id]
      logger.debug "call find_by_name_and_run_id"
      @results = Result.find_by_name_and_run_id(params[:name], params[:run_id], @latest, find_params)
   elsif !params[:test_id].nil? and !params[:run_id].nil?
      @test_id = params[:test_id]
      @run_id = params[:run_id]
      logger.debug "call find_by_test_id_and_run_id"
      @results = Result.find_by_test_id_and_run_id(params[:test_id], params[:run_id], @latest, find_params)
    elsif !params[:test_id].nil?
      @test_id = params[:test_id]
      logger.debug "call find_by_test_id"      
      @results = Result.find_by_test_id(params[:test_id], @latest, find_params)
    elsif !params[:run_id].nil?
      @run_id = params[:run_id]
      logger.debug "call find_by_run_id"
      @results = Result.find_by_run_id(params[:run_id], @latest, find_params)
    else
      @results = Result.find :all, find_params
    end

    @results.each do | r |
        if r.passed.include?('true')
          @passed = @passed + 1
        else
          @failed = @failed + 1
        end
    end
    @total = @passed + @failed

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @results }
    end
  end

  # GET /results/1
  # GET /results/1.xml
  def show
    @result = Result.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @result }
    end
  end

  # GET /results/new
  # GET /results/new.xml
  def new
    @result = Result.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @result }
    end
  end

  # GET /results/1/edit
  def edit
    @result = Result.find(params[:id])
  end

  # POST /results
  # POST /results.xml
  def create
    @result = Result.new(params[:result])

    respond_to do |format|
      if @result.save
        flash[:notice] = 'Result was successfully created.'
        format.html { redirect_to(@result) }
        format.xml  { render :xml => @result, :status => :created, :location => @result }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @result.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /results/1
  # PUT /results/1.xml
  def update
    @result = Result.find(params[:id])

    respond_to do |format|
      if @result.update_attributes(params[:result])
        flash[:notice] = 'Result was successfully updated.'
        format.html { redirect_to(@result) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @result.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /results/1
  # DELETE /results/1.xml
  def destroy
    @result = Result.find(params[:id])
    @result.destroy

    respond_to do |format|
      format.html { redirect_to(results_url) }
      format.xml  { head :ok }
    end
  end
end
